import datetime

class timer:
    percent = -1
    start_time = -1
    begin_frame = -1
    end_frame = -1
    draw_date = False
    tens = False
    def __init__(self, begin=None, end=None, date=False, tens=False):
        self.begin_frame = begin
        self.end_frame = end
        self.start_time = datetime.datetime.now()
        self.percent = -1
        self.draw_date = date
        self.tens = tens
        # print 'start time', str(self.start_time)

    def progress(self, i):
        perc_f = (i - self.begin_frame+1)/float(self.end_frame - self.begin_frame+1) * 100.
        perc = int(round(perc_f, 0))
        if perc > self.percent:
            if (self.tens and perc % 10 == 0) or not self.tens:
                self.percent = perc
                elapsed = datetime.datetime.now() - self.start_time
                print "%d%%" % self.percent
                if self.percent != 0:
                    if self.draw_date:
                        print 'end time = %s' % (self.start_time + datetime.timedelta(seconds=elapsed.total_seconds() / (perc_f/100.)))
                    else:
                        print 'end time = %s' % str((self.start_time + datetime.timedelta(seconds=elapsed.total_seconds() / (perc_f/100.))).time())[:8]


    def elapsed(self):
        return str(datetime.datetime.now() - self.start_time)

    def reset(self):
        self.start_time = datetime.datetime.now()

if __name__ == '__main__':
    tm = timer()
    for i in range(10000000): continue
    tm.elapsed()
